package cn.codexin.wechatofficial.controller;

import cn.codexin.wechatofficial.mapper.HTDB.dbo.CommonMapper;
import cn.codexin.wechatofficial.mapper.HTDB.dbo.StationMapper;
import cn.codexin.wechatofficial.model.dbo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ly
 * @date 2020/8/14 0014 0:31
 * 各页面频繁使用的API
 */
@RestController
@RequestMapping(value = "common", produces = "application/json;charset=UTF-8")
public class CommonController {
    /*CommonController 由于使用频繁，没有设置service层*/
    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private StationMapper stationMapper;

    /*获得地区列表*/
    @GetMapping("allAreas")
    public Map<String,Object> allAreas(){
        Map<String,Object> map = new HashMap<>();
        try {
            List<AreaPojo> areaPojoList = commonMapper.allAreas();
            map.put("state",true);
            map.put("msg","查询成功");
            map.put("areaPojoList",areaPojoList);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }

    /*获得指定地区的分站列表*/
    @GetMapping("SubstationsByAreaID")
    public Map<String,Object> SubstationsByAreaID(@RequestParam int AreaID){
        Map<String,Object> map = new HashMap<>();
        try {
            List<SubstationPojo> substationPojoList = commonMapper.SubstationsByAreaID(AreaID);
            if (substationPojoList.size()==0){
                throw new RuntimeException("该地区还未设置分站,请联系管理员！");
            }else {
                map.put("state",true);
                map.put("msg","查询成功");
                map.put("substationPojoList",substationPojoList);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }

    /*获得当前站点信息*/
    @GetMapping("currentStation")
    public Map<String,Object> currentStation(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap<>();
        try {
            //获得当前用户的open_id
            String openid = request.getHeader("openid");
            //获得当前分站
            SubstationPojo currentStation = commonMapper.getCurrentStation(openid);
            //获得当前地区
            AreaPojo currentArea = commonMapper.getCurrentArea(currentStation.getSubstationID());
            map.put("state",true);
            map.put("msg","查询成功");
            map.put("currentArea",currentArea);
            map.put("currentStation",currentStation);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }
    /*查询当前站点的所有房间*/
    @GetMapping("allModules")
    public Map<String,Object> allModules(@RequestParam int SubstationID){
        Map<String,Object> map = new HashMap<>();
        try {
            //获得当前分站的IEDModules
            List<IEDModulePojo> modulePojoList = commonMapper.getAllModules(SubstationID);
            if (modulePojoList.size()==0){
                throw new RuntimeException("该站点还未设置房间,请联系管理员！");
            }else {
                map.put("state",true);
                map.put("msg","查询成功");
                map.put("modulePojoList",modulePojoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","查询失败,提示："+e.getMessage());
        }
        return map;
    }
    // ZF
    //通过openid查询该用户的所有已授权站点(用于切换站点)
    @GetMapping("stationByOpenid")
    public Map<String, Object> stationByOpenid(@RequestParam(value = "openid", required = true) String openid) {
        Map<String, Object> map = new HashMap<>();
        try {
            StationExample stationExample = new StationExample();
            stationExample.createCriteria().andOpenidEqualTo(openid);
            List<Station> stations = stationMapper.selectByExample(stationExample);
            map.put("state", true);
            map.put("msg", "查询成功");
            map.put("stationList", stations);
        } catch (RuntimeException e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "查询失败,提示：" + e.getMessage());
        }
        return map;
    }

}
